<article id="wikiArticle">
<div></div>
<p><strong>Symbol.iterator </strong>为每一个对象定义了默认的迭代器。该迭代器可以被 <code><a href="Reference/Statements/for...of">for...of</a></code> 循环使用。</p>
<div><table class="standard-table">
<thead>
<tr>
<th class="header" colspan="2"><code>Symbol.iterator</code> 属性的属性特性：</th>
</tr>
</thead>
<tbody>
<tr>
<td>writable</td>
<td>false</td>
</tr>
<tr>
<td>enumerable</td>
<td>false</td>
</tr>
<tr>
<td>configurable</td>
<td>false</td>
</tr>
</tbody>
</table></div>
<h2 id="描述">描述</h2>
<p>当需要对一个对象进行迭代时（<font><font>比如开始用于一个</font></font><code><font><font>for..of</font></font></code><font><font>循环中</font></font>），它的<code>@@iterator</code>方法都会在不传参情况下被调用，返回的<strong>迭代器</strong>用于获取要迭代的值。</p>
<p>一些内置类型拥有默认的迭代器行为，其他类型（如 <a href="Reference/Global_Objects/Object" title="Object 构造函数创建一个对象包装器。"><code>Object</code></a>）则没有。下表中的内置类型拥有默认的<span style="font-family: consolas,monaco,andale mono,monospace;"><code>@@iterator</code></span>方法：</p>
<ul>
<li><a href="Reference/Global_Objects/Array/@@iterator" title="@@iterator 属性和 Array.prototype.values() 属性的初始值均为同一个函数对象。"><code>Array.prototype[@@iterator]()</code></a></li>
<li><a href="Reference/Global_Objects/TypedArray/@@iterator" title="@@iterator 的初始值是和 values 属性的初始值相同的对象。"><code>TypedArray.prototype[@@iterator]()</code></a></li>
<li><a href="Reference/Global_Objects/String/@@iterator" title="[@@iterator]() 方法返回一个新的Iterator对象，它遍历字符串的代码点，返回每一个代码点的字符串值。"><code>String.prototype[@@iterator]()</code></a></li>
<li><a href="Reference/Global_Objects/Map/@@iterator" title="@@iterator 属性的初始值与 entries 属性的初始值是同一个函数对象。"><code>Map.prototype[@@iterator]()</code></a></li>
<li><a href="Reference/Global_Objects/Set/@@iterator" title="The initial value of the @@iterator property is the same function object as the initial value of the values property."><code>Set.prototype[@@iterator]()</code></a></li>
</ul>
<p>更多信息请参见<a href="https://developer.mozilla.orgReference/Iteration_protocols">迭代协议</a>。</p>
<h2 id="示例">示例</h2>
<h3 id="自定义迭代器">自定义迭代器</h3>
<p>我们可以像下面这样创建自定义的迭代器：</p>
<pre><code  class="language-javascript">var myIterable = {}
myIterable[Symbol.iterator] = function* () {
    yield 1;
    yield 2;
    yield 3;
};
[...myIterable] // [1, 2, 3]
</code></pre>
<h3 id="不符合标准的迭代器">不符合标准的迭代器</h3>
<p>如果一个迭代器 <code>@@iterator</code> 没有返回一个迭代器对象，那么它就是一个不符合标准的迭代器，这样的迭代器将会在运行期抛出异常，甚至非常诡异的 Bug。</p>
<pre><code  class="language-javascript">var nonWellFormedIterable = {}
nonWellFormedIterable[Symbol.iterator] = () =&gt; 1
[...nonWellFormedIterable] // TypeError: [] is not a function
</code></pre>
<h2 id="规范">规范</h2>
<table class="standard-table">
<tbody>
<tr>
<th scope="col">Specification</th>
<th scope="col">Status</th>
<th scope="col">Comment</th>
</tr>
<tr>
<td><a class="external" href="https://www.ecma-international.org/ecma-262/6.0/#sec-symbol.iterator" hreflang="en" lang="en" rel="noopener">ECMAScript 2015 (6th Edition, ECMA-262)<br/><small lang="zh-CN">Symbol.iterator</small></a></td>
<td><span class="spec-Standard">Standard</span></td>
<td>Initial definition.</td>
</tr>
<tr>
<td><a class="external" href="https://tc39.github.io/ecma262/#sec-symbol.iterator" hreflang="en" lang="en" rel="noopener">ECMAScript Latest Draft (ECMA-262)<br/><small lang="zh-CN">Symbol.iterator</small></a></td>
<td><span class="spec-Draft">Draft</span></td>
<td> </td>
</tr>
</tbody>
</table>
<h2 id="浏览器兼容性">浏览器兼容性</h2>
<div class="hidden">The compatibility table on this page is generated from structured data. If you'd like to contribute to the data, please check out <a class="external" href="https://github.com/mdn/browser-compat-data" rel="noopener">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</div>
<p></p><div class="bc-data"><a class="bc-github-link external" href="https://github.com/mdn/browser-compat-data" rel="noopener">Update compatibility data on GitHub</a><table class="bc-table bc-table-js"><thead><tr class="bc-platforms"><td></td><th class="bc-platform-desktop" colspan="6"><span>Desktop</span></th><th class="bc-platform-mobile" colspan="7"><span>Mobile</span></th><th class="bc-platform-server" colspan="1"><span>Server</span></th></tr><tr class="bc-browsers"><td></td><th class="bc-browser-chrome"><span class="bc-head-txt-label bc-head-icon-chrome">Chrome</span></th><th class="bc-browser-edge"><span class="bc-head-txt-label bc-head-icon-edge">Edge</span></th><th class="bc-browser-firefox"><span class="bc-head-txt-label bc-head-icon-firefox">Firefox</span></th><th class="bc-browser-ie"><span class="bc-head-txt-label bc-head-icon-ie">Internet Explorer</span></th><th class="bc-browser-opera"><span class="bc-head-txt-label bc-head-icon-opera">Opera</span></th><th class="bc-browser-safari"><span class="bc-head-txt-label bc-head-icon-safari">Safari</span></th><th class="bc-browser-webview_android"><span class="bc-head-txt-label bc-head-icon-webview_android">Android webview</span></th><th class="bc-browser-chrome_android"><span class="bc-head-txt-label bc-head-icon-chrome_android">Chrome for Android</span></th><th class="bc-browser-edge_mobile"><span class="bc-head-txt-label bc-head-icon-edge_mobile">Edge Mobile</span></th><th class="bc-browser-firefox_android"><span class="bc-head-txt-label bc-head-icon-firefox_android">Firefox for Android</span></th><th class="bc-browser-opera_android"><span class="bc-head-txt-label bc-head-icon-opera_android">Opera for Android</span></th><th class="bc-browser-safari_ios"><span class="bc-head-txt-label bc-head-icon-safari_ios">Safari on iOS</span></th><th class="bc-browser-samsunginternet_android"><span class="bc-head-txt-label bc-head-icon-samsunginternet_android">Samsung Internet</span></th><th class="bc-browser-nodejs"><span class="bc-head-txt-label bc-head-icon-nodejs">Node.js</span></th></tr></thead><tbody><tr><th scope="row"><code>iterator</code></th><td class="bc-supports-yes bc-browser-chrome"><span class="bc-browser-name">Chrome</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              43</td><td class="bc-supports-yes bc-browser-edge"><span class="bc-browser-name">Edge</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              12</td><td class="bc-supports-yes bc-browser-firefox"><span class="bc-browser-name">Firefox</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              36</td><td class="bc-supports-no bc-browser-ie"><span class="bc-browser-name">IE</span><abbr class="bc-level-no only-icon" title="No support">
<span>No support</span>
</abbr>
              No</td><td class="bc-supports-yes bc-browser-opera"><span class="bc-browser-name">Opera</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              30</td><td class="bc-supports-yes bc-browser-safari"><span class="bc-browser-name">Safari</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              10</td><td class="bc-supports-yes bc-browser-webview_android"><span class="bc-browser-name">WebView Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              43</td><td class="bc-supports-yes bc-browser-chrome_android"><span class="bc-browser-name">Chrome Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              43</td><td class="bc-supports-yes bc-browser-edge_mobile"><span class="bc-browser-name">Edge Mobile</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-firefox_android"><span class="bc-browser-name">Firefox Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              36</td><td class="bc-supports-yes bc-browser-opera_android"><span class="bc-browser-name">Opera Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-safari_ios"><span class="bc-browser-name">Safari iOS</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              10</td><td class="bc-supports-yes bc-browser-samsunginternet_android"><span class="bc-browser-name">Samsung Internet Android</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              Yes</td><td class="bc-supports-yes bc-browser-nodejs"><span class="bc-browser-name">nodejs</span><abbr class="bc-level-yes only-icon" title="Full support">
<span>Full support</span>
</abbr>
              0.12</td></tr></tbody></table><section class="bc-legend" id="sect1"><h3 class="offscreen" id="Legend">Legend</h3><dl><dt><span class="bc-supports-yes bc-supports">
<abbr class="bc-level bc-level-yes only-icon" title="Full support">
<span>Full support</span>
                  
                </abbr></span></dt><dd>Full support</dd><dt><span class="bc-supports-no bc-supports">
<abbr class="bc-level bc-level-no only-icon" title="No support">
<span>No support</span>
                  
                </abbr></span></dt><dd>No support</dd></dl></section></div><p></p>
<h2 id="相关链接">相关链接</h2>
<ul>
<li><a href="https://developer.mozilla.orgReference/Iteration_protocols">迭代协议</a></li>
<li><a href="Reference/Global_Objects/Array/@@iterator" title="@@iterator 属性和 Array.prototype.values() 属性的初始值均为同一个函数对象。"><code>Array.prototype[@@iterator]()</code></a></li>
<li><a href="Reference/Global_Objects/TypedArray/@@iterator" title="@@iterator 的初始值是和 values 属性的初始值相同的对象。"><code>TypedArray.prototype[@@iterator]()</code></a></li>
<li><a href="Reference/Global_Objects/String/@@iterator" title="[@@iterator]() 方法返回一个新的Iterator对象，它遍历字符串的代码点，返回每一个代码点的字符串值。"><code>String.prototype[@@iterator]()</code></a></li>
<li><a href="Reference/Global_Objects/Map/@@iterator" title="@@iterator 属性的初始值与 entries 属性的初始值是同一个函数对象。"><code>Map.prototype[@@iterator]()</code></a></li>
<li><a href="Reference/Global_Objects/Set/@@iterator" title="The initial value of the @@iterator property is the same function object as the initial value of the values property."><code>Set.prototype[@@iterator]()</code></a></li>
</ul>
</article>